package es.wandererteam.tapakitty.management;

import org.andengine.engine.Engine;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.ui.IGameInterface.OnCreateSceneCallback;

import es.wandererteam.tapakitty.GameScene;
import es.wandererteam.tapakitty.LoadingScene;
import es.wandererteam.tapakitty.MainMenuScene;
import es.wandererteam.tapakitty.ObjectScene;
import es.wandererteam.tapakitty.ShopScene;
import es.wandererteam.tapakitty.SplashScene;


public class SceneManager
{
    //---------------------------------------------
    // SCENES
    //---------------------------------------------
    
    private BaseScene splashScene;
    private BaseScene menuScene;
    private BaseScene gameScene;
    private BaseScene loadingScene;
    
    private BaseScene shopScene;
    private BaseScene objectScene;
    
    //---------------------------------------------
    // VARIABLES
    //---------------------------------------------
    
    private static final SceneManager INSTANCE = new SceneManager();
    
    private SceneType currentSceneType = SceneType.SCENE_SPLASH;
    
    private BaseScene currentScene;
    
    private Engine engine = ResourcesManager.getInstance().engine;
    
    public enum SceneType
    {
        SCENE_SPLASH,
        SCENE_MENU,
        SCENE_GAME,
        SCENE_LOADING,
        SCENE_SHOP,
        SCENE_OBJECT
    }
    
  //---------------------------------------------
    // GETTERS AND SETTERS
    //---------------------------------------------
    
    public static SceneManager getInstance()
    {
    	return INSTANCE;
    }
    
    public SceneType getCurrentSceneType()
    {
        return currentSceneType;
    }
    
    public BaseScene getCurrentScene()
    {
        return currentScene;
    }
    
    //---------------------------------------------
    // CLASS LOGIC
    //---------------------------------------------
    
    public void setScene(BaseScene scene)
    {
        engine.setScene(scene);
        currentScene = scene;
        currentSceneType = scene.getSceneType();
    }
    
    public void setScene(SceneType sceneType)
    {
        switch (sceneType)
        {
            case SCENE_MENU:
                setScene(menuScene);
                break;
            case SCENE_GAME:
                setScene(gameScene);
                break;
            case SCENE_SPLASH:
                setScene(splashScene);
                break;
            case SCENE_LOADING:
                setScene(loadingScene);
                break;
            case SCENE_SHOP:
                setScene(shopScene);
                break;
            case SCENE_OBJECT:
                setScene(objectScene);
                break;
            default:
                break;
        }
    }
    
    public void createSplashScene(OnCreateSceneCallback pOnCreateSceneCallback)
    {
        ResourcesManager.getInstance().loadSplashScreen();
        splashScene = new SplashScene();
        currentScene = splashScene;
        pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
    }
    private void disposeSplashScene()
    {
        ResourcesManager.getInstance().unloadSplashScreen();
        splashScene.disposeScene();
        splashScene = null;
    }
    
    public void createMenuScene()
    {
    	ResourcesManager.getInstance().loadMenuResources();
    	menuScene = new MainMenuScene();
    	loadingScene = new LoadingScene();
    	SceneManager.getInstance().setScene(menuScene);
    	disposeSplashScene();
    }
    
    private void disposeMenuScene()
    {
       // ResourcesManager.getInstance().unloadMenuScene();
        menuScene.disposeScene();
        menuScene = null;
    }
    public void loadMenuScene(final Engine mEngine)
    {
    	setScene(loadingScene);
   		gameScene.disposeScene();//si vengo del choose scene dara error
   		ResourcesManager.getInstance().unloadGameTextures();
   		mEngine.registerUpdateHandler(new TimerHandler(0.2f, new ITimerCallback() 
   		{
   			public void onTimePassed(final TimerHandler pTimerHandler) 
   			{
   				mEngine.unregisterUpdateHandler(pTimerHandler);
   				ResourcesManager.getInstance().loadMenuTextures();
   				setScene(menuScene);
   			}
   		}));
    }
    
    public void loadShopScene(final Engine mEngine)
    {
    	//setScene(loadingScene);
   		gameScene.disposeScene();
   		ResourcesManager.getInstance().unloadGameTextures();
   		mEngine.registerUpdateHandler(new TimerHandler(0.2f, new ITimerCallback() 
   		{
   			public void onTimePassed(final TimerHandler pTimerHandler) 
   			{
   				mEngine.unregisterUpdateHandler(pTimerHandler);
   				ResourcesManager.getInstance().loadShopResources();
   				shopScene = new ShopScene();
   				setScene(shopScene);
   			}
   		}));
    }
    public void loadObjectScene(final Engine mEngine)
    {
    	//setScene(loadingScene);
   		gameScene.disposeScene();
   		ResourcesManager.getInstance().unloadGameTextures();
   		mEngine.registerUpdateHandler(new TimerHandler(0.2f, new ITimerCallback() 
   		{
   			public void onTimePassed(final TimerHandler pTimerHandler) 
   			{
   				mEngine.unregisterUpdateHandler(pTimerHandler);
   				ResourcesManager.getInstance().loadObjectResources();
   				objectScene = new ObjectScene();
   				setScene(objectScene);
   			}
   		}));
    }
   
    public void loadGameScene(final Engine mEngine)
    {
    	//setScene(loadingScene);
    	
        mEngine.registerUpdateHandler(new TimerHandler(0.5f, new ITimerCallback() 
        {
            public void onTimePassed(final TimerHandler pTimerHandler) 
            {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                ResourcesManager.getInstance().loadGameResources();
                gameScene = new GameScene();
                setScene(gameScene);
            }
        }));
    }

  
    
}